#include <stdio.h>

void heapify(int *tree, int n, int parent)
{
    if (parent >= n)
    {
        return;
    }

    // 父节点最大
    int max = parent;
    int left = 2 * parent + 1;
    int right = 2 * parent + 2;
    // 找到最大值
    if (left < n && tree[left] > tree[max])
    {
        max = left;
    }
    if (right < n && tree[right] > tree[max])
    {
        max = right;
    }
    if (max != parent)
    {
        int t = tree[parent];
        tree[parent] = tree[max];
        tree[max] = t;
        heapify(tree, n, max);
    }
}

int main()
{
    int arr[] = {4, 10, 3, 5, 1, 2};
    int n = 6;
    heapify(arr, n, 0);
    // printf("%d", 5 / 2);
    for (int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }

    return 0;
}